<?php

/* @var $this \yii\web\View */
/* @var $content string */

use yii\helpers\Html;
use front\assets\AppAsset;
use yii\helpers\Url;
$this->title = '提交订单';

header("Expires:0");
header("Cache-Control: no-cache");
header("Pragma: no-cache");

?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>购买</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0" />
    <meta name="format-detection" content="telephone=no,email=no" />
    <meta name="mobileOptimized" content="width" />
    <meta name="handheldFriendly" content="true" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <link rel="stylesheet" href="/static/css/main.css" />
    <script src="/static/script/zepto.min.js"></script>
    <style>body,html{height:100%;width:100%;}</style>

</head>
<body>
<div class="m-buy" data-uid="<?php echo $uid ?>">

    <?php if($list['user_site']){ ?>
        <div class="u-adrs adds" id="J-address" data-site="<?php if(isset($list['user_site'][0]['id'])){echo $list['user_site'][0]['id'];}?>">
            <h5><span id="j-edit">收货地址</span><em id="j-addresschar"><?php if(isset($list['user_site'][0]['address'])){echo $list['user_site'][0]['address'];}?></em></h5>
            <p><em  id="j-name">收货人: <?php if(isset($list['user_site'][0]['name'])){echo $list['user_site'][0]['name'];}?></em>
                <span id="j-mobile"><?php if(isset($list['user_site'][0]['mobile'])){echo $list['user_site'][0]['mobile'];}?></span>
            </p>
        </div>
    <?php }else{ ?>
        <div class="u-adrs adds" id="J-address" data-site="">
            <h5><span id="j-edit">收货地址</span><em id="j-addresschar"></em></h5>
            <p><em  id="j-name"></em><span id="j-mobile"></span></p>
        </div>
    <?php } ?>
    <input type="hidden" value="" name='district' id="j-district">
    <input type="hidden" value="" name='addr_default' id="j-addr_default">

    <?php foreach($list['product'] as $k=>$v){?>
        <div class="desc order-desc" data-shop="<?php echo $v['shop_id'];?>" data-pid="<?php echo $v['product_id'];?>">
            <a href="<?= Url::to(['product/detail','pid' => $v['product_id']])?>">
                <img src="<?php if(isset($v['data']['thumb'])){ echo $v['data']['thumb'];}?>"/>
            </a>
            <h4>
                <a href="<?= Url::to(['product/detail','pid' => $v['product_id']])?>"><?php echo $v['name'];?></a><br />
                <?php echo $v['data']['item_attrText_map'];?>
                <em>数量：</em><i id="buy-num"><?php echo $v['num'];?></i>
            </h4>
            <label>￥ <b><?php echo $v['price'];?></b></label>
        </div>
    <?php } ?>

    <div class="u-swit J_switch pay-info">
        <h4><span>支付方式</span><a href="javascript:;" class="j-pay-express"><?php if($selfaddr){ echo $selfaddrInfo;}else{ echo '线上支付';}?></a>
            <input value="<?php if($selfaddr){ echo '2';}else{ echo '1';}?>" type="hidden" /></h4>
        <p data-id="1" class="<?php if($selfaddr){echo ''; }else{ echo 'z-on';}?>" id="J_payOnline">线上支付</p>
        <p data-id="2" class="<?php if($selfaddr){echo 'z-on'; }else{ echo '';}?>" id="J_payOffline">线下支付</p>
    </div>

    <div class="u-swit J_switch express-in">
        <h4><span>配送方式</span><a href="javascript:;" class="j-express"><?php if($selfaddr&&$selfPersonal){ echo '自提';}else{ echo '快递';}?></a>
        <input value="<?php if($selfDelivery){ echo '3';}else{ echo '4';}?>" id="J_deliveryType" type="hidden" /></h4>
        <?php if(!$onlinepay&&!$selfaddr){?>
            <p data-id="3" class="z-on" id="J_delivery">快递</p>
            <p data-id="4" id="J_address-self" style="display: none">自提</p>
        <?php }elseif($selfaddr){ ?>
            <p data-id="3" class="z-on" id="J_delivery">快递</p>
            <p data-id="4" id="J_address-self" style="display: none">自提</p>
        <?php }elseif($selfaddrInfo&&$onlinepay){ ?>
            <p data-id="3" class="z-on" id="J_delivery">快递</p>
            <p data-id="4" id="J_address-self" style="display: none">自提</p>
        <?php }elseif($onlinepay){ ?>
            <p data-id="3" class="z-on" id="J_delivery">快递</p>
        <?php }elseif($selfaddr&&$selfDelivery){ ?>
            <p data-id="3" class="z-on" id="J_delivery">快递</p>
        <?php }elseif($selfaddr&&$selfPersonal){ ?>
            <p data-id="4" class="z-on" id="J_address-self" style="display: none">自提</p>
        <?php } ?>
    </div>

    <div class="u-swit J_switch express-time u-swit-spe">
        <?php if($weekDay||$workDay){?>
        <h4><span>送货时间</span><a href="javascript:;"><?php if($weekDay){ echo '只双休日、节假日送货';}else{ echo '只工作日送货';}?></a>
        <input value="<?php if($weekDay){ echo '6';}else{ echo '5';}?>" type="hidden" /></h4>
        <p data-id="5" class="<?php if($workDay){echo 'z-on'; }else{ echo '';}?>" >只工作日送货</p>
        <p data-id="6" class="<?php if($weekDay){echo 'z-on'; }else{ echo '';}?>" >只双休日、节假日送货</p>
        <?php } else{ ?>
            <h4><span>送货时间</span><a href="javascript:;">只双休日、节假日送货</a>
            <input value="6" type="hidden" /></h4>
            <p data-id="5">只工作日送货</p>
            <p data-id="6"  class="z-on">只双休日、节假日送货</p>
        <?php }?>
    </div>

    <h5 class="u-fat" id="J_coupon"><span>优惠券</span><a href="javascript:;"><?php if(isset($userCoupons)&&$userCoupons&&empty($couponsVal)){ echo $userCoupons['describle']; }elseif(isset($couponsVal)&&$couponsVal){ echo '-￥'.$couponsVal;}else{ echo "无可用优惠券";} ?></a></h5>

    <h5 class="u-fat" id="J_remark"><span>备注</span><a href="javascript:;"><?php echo $remark;?></a></h5>

    <ul class="m-price">
        <li><b>总价</b><span>￥<?php echo $list['total_price']?></span></li>
        <li><b>优惠</b><span id="J_couponInfo"><?php if(isset($couponsVal)&&$couponsVal){ echo '-￥'.$couponsVal;}?></span></li>
        <input type="hidden" id="J-get-coupons" attr_id="<?php if(isset($couponsId)&&$couponsId){ echo $couponsId;}?>" value="<?php if(isset($couponsVal)&&$couponsVal){ echo $couponsVal;}?>">
<!--        <li><b>运费</b><span>+ ￥0.00</span></li>-->
    </ul>


    <dl class="m-resu f-cb">
        <dt>您需要支付：<b>￥<i id="J_total_info"><?php if(isset($couponsVal)&&$couponsVal){ $total_coupons = ($list['total_price']-$couponsVal); echo \front\controllers\WebBaseController::price_format($total_coupons);}else{ echo \front\controllers\WebBaseController::price_format($list['total_price']);} ?></i></b></dt>
        <dd><a href="javascript:;" class="orderBuy z-ajaxbuy"  data-buy="<?php echo $hide_buy;?>">购买</a></dd>
    </dl>
</div>

<!-- 优惠券 -->

<div class="m-transX" id="J_actCoupon">

    <iframe class="m-iframe" src="/web/order/coupons" style="" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"></iframe>
</div>



<div class="m-transX" id="J_trans_remark" style="background-color:#ebf0f2;">
    <div class="cnt m-remark">
        <textarea placeholder="请填写信息" rows="4"><?php echo $remark;?></textarea>
        <a href="javascript:;" id="J_remarkSure"><span>确定</span></a>
    </div>
</div>

<form action="/web/address/self-address?from=submit" method="post" id="subform"></form>

<script>

    $(function(){

        // 滑动的基础样式
        var transX = 'z-transX';

        var switchTag = $('.J_switch'), zon = 'z-on';
        switchTag.on('tap', 'a', function(){

            var _that = $(this), _parent = _that.parent('h4');

            if(_parent.hasClass(zon)){
                _parent.removeClass(zon).siblings().hide();
            }else{
                _parent.addClass(zon).siblings().show();
            }

            var _idx = switchTag.find('a').index(_that);

            var _v = $('.pay-info input').val();
            // 点击配送方式
            if(_idx === 1){
                if(_v === '1'){
                    $('#J_address-self').hide();
                }
            }

        });

        // 选择
        switchTag.on('tap', 'p', function(){
            var _that = $(this), _parent = _that.parent('div'), _id = _that.attr('data-id'), _inputval = $('.pay-info input').val();
//            //线上支付
            if(_inputval == '1' ){
                $('#J_delivery').addClass(zon);
                $('#J_address-self').removeClass(zon);
            }


            //收货方式
            if( _id === '2' ){
                $('#J_addPayOffAddressSelf').addClass(transX);
            }
            if(_that.hasClass(zon)){
                _parent.find('p').hide();
                _parent.find('h4').removeClass(zon);
                return false;
            }else{
                _that.addClass(zon).siblings().removeClass(zon);
                _parent.find('a').text(_that.text()).next().val(_that.attr('data-id'));
                _parent.find('h4').removeClass(zon).siblings().hide();

            }


            $('#J_delivery').hide();
            $('#J_address-self').hide().parent().find('h4').removeClass(zon);

            if(_that.attr('id')  === 'J_payOnline' && $('#J_deliveryType').val() === '4'){
                $('.j-express').text('快递').next().val(1);
               // $('#J_delivery').addClass(zon);
            }
            $.post('/web/order/seltag',{tagid:_id},function(data){

            },'json');
        });

        // Tab切换
        var sltBar = $('#J_sltBar').children('span'),
            sltCnt = $('.J_sltCnt'),
            zon = 'z-on';
        sltBar.on('touchend', function(evt){

            var _that = $(this);

            if(_that.hasClass(zon)) return !1;

            var _i = _that.index();

            sltBar.removeClass(zon).eq(_i).addClass(zon);

            var _html = '';

            // 动态加载数据
            $.ajax({ url: 'ajax.coupon.php?funcName=?', dataType:'jsonp', data:{'type':_i}, timeout:5000,

                success: function(_info){

                    console.log(_info);

                    _html = createList(_info);

                    sltCnt.hide().eq(_i).html(_html).show();
                }
            });

            evt.preventDefault();
            return false;
        });

        // 显示 修改备注 滑板
        $('#J_remark').on('tap', function(){
            $('#J_trans_remark').addClass(transX);
        });

        //地址列表
        // transform
        $('#J-address').on('touchend', function(evt){

            window.location.href = "/web/order/addaddr";

            evt.preventDefault();
            return false;
        });

        //跳转到自提地址
        $('#J_payOffline').on('tap', function(){
            window.location.href = "/web/order/addaddrself";
        });


        // 修改备注 隐藏 滑板
        $('#J_trans_remark').on('touchend', 'a', function(evt){
            $('#J_remark a').text($(this).prev().val());
            $('#J_trans_remark').removeClass(transX);
            evt.preventDefault();
            return false;
        });

        $('#J_remarkSure').on('touchend', function(evt){
            var remark =  $(this).prev().val();
            $.post('/web/order/addremark',{remark:remark},function(data){

            },'json')

        });


        // 优惠券
        $('#J_coupon').on('tap', function(){
            $('#J_actCoupon').addClass(transX);
        });

        //提交订单
        var shop_id=[],pid=[],address,pay,express,express_time,remark;
        $('.orderBuy').tap(function(){
            if(!$(this).hasClass('z-ajaxbuy')) return false;
            //商品的信息
            $('.order-desc').each(function(k,v){
                shop_id.push($(this).attr('data-shop'));
                pid.push($(this).attr('data-pid'));

            });

            //支付方式
            pay = $('.pay-info h4 input').val();

            //获取地址信息
            address=$('.adds').attr('data-site');

            if(!address){
                msgPopup('请选择收货地址');
                return false;
            }
            var hide_buy = $(this).attr('data-buy');
            var isdefaddress = $('#j-addr_default').val();

            //快递方式
            express = $('.express-in a').text();
            //自提
            var express_self = $('.j-pay-express').text()
            //送货时间
            express_time = $('.express-time a').text();

            //卡券信息
            var coupons = $("#J-get-coupons").val();
            var coupons_id = $("#J-get-coupons").attr('attr_id');

            var buy_num = $('#buy-num').text();
            //备注
            remark = $('#J_remark a').text();
            var data={shop_id:shop_id,pid:pid,pay:pay,address:address,hide_buy:hide_buy,isdefaddress:isdefaddress,
                express:express,express_self:express_self,express_time:express_time,remark:remark,buy_num:buy_num,coupons:coupons,coupons_id:coupons_id
            }
            $('.orderBuy').removeClass('z-ajaxbuy');
            $.post('/web/order/create',data,function(data){
                if(data.status==1){
                    if(data.pay_type==2){
                        setTimeout(function() {
                            window.location.href='/web/order/confirminfo?order_id='+data.order_id;
                        },1500);
                    }else{
                        setTimeout(function() {
                            window.location.href='/web/order/payment?order_id='+data.order_id;
                        },1500);
                    }
                }else if(data.status==7){
                    msgPopup('库存不足');
                    setTimeout(function() {
                        window.location.href='/web/product/detail?pid='+data.pid;
                    },3000);
                }else if(data.status==8){
                    msgPopup('库存不足');
                    setTimeout(function() {
                        window.location.href='/web/cart/view';
                    },3000);
                }else{
                    msgPopup(data.message);
                }
            },'json')

        })
    });

    // 动态创建列表
    function createList(info){

        var _html = '';

        for(var i in info){

            var rs = info[i];

            var canUsed = '已使用';
            if(rs['leave'] > 0){
                canUsed = '可使用';
            }else if(rs['leave'] == 0){
                canUsed = '已过期';
            }
            var leave = rs['leave'] > 0 ? '<small>(剩余 '+ rs['leave'] +' 天)</small>' : '';

            _html += '<div class="f-cb u-spe'+ rs['class'] +'">' +
                '<img src="img/bg0'+ rs['class'] +'.png" />' +
                '<span>'+ rs['type'] +'</span>' +
                '<h4><b>'+ rs['money'] +'</b>元<br /><small>('+ rs['desc'] +')</small><br /><em>'+ rs['date'] +'</em></h4>' +
                '<h5><b>'+ canUsed +'</b><br />'+ leave +'</h5>' +
                '</div>';
        }

        if(_html === '') _html = '<h6>您还没有优惠券</h6>';

        return _html;
    }

</script>

</body>